package com.springboot.controller;

import com.springboot.domain.Result;
import com.springboot.dto.AddCommentDTO;
import com.springboot.enums.ResultEnum;
import com.springboot.repository.CommentRepository;
import com.springboot.service.CommentService;
import com.springboot.tools.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
public class CommentController {

    private CommentService commentService;
    private CommentRepository commentRepository;
    @Autowired
    public CommentController(CommentService commentService,CommentRepository commentRepository) {
        this.commentService = commentService;
        this.commentRepository = commentRepository;
    }


    //新增评价
    @ResponseBody
    @PostMapping(value = "/bank/commnent/add")
    public Result addComment(@RequestBody AddCommentDTO addCommentDTO, HttpSession session){
        String userId = (String) session.getAttribute("user");
        if( userId == null){
            return ResultUtil.error(ResultEnum.NOT_LOGIN);
        }
        commentService.addComment(addCommentDTO, userId);
        return ResultUtil.success("已评价");
    }

    //根据项目获取所有评价
    @ResponseBody
    @GetMapping(value = "/bank/comments/{demandId}")
    public Result getAllComment(@PathVariable("demandId") String demandId){
        return ResultUtil.success(commentRepository.findByDemandIdOrderByFloor(demandId));
    }

}
